<template>
  <el-menu
    active-text-color="#409EEF"
    background-color="#32363f"
    text-color="#fff"
    unique-opened
    :collapse-transition="false"
    router
    :default-active="defaultActive"
    :collapse="$store.state.isCollapse"
  >
    <!-- 一级菜单 -->
    <el-sub-menu :index="item.id + ''" v-for="item in menus" :key="item.id">
      <!-- 一级菜单内容 -->
      <template #title>
        <el-icon>
          <component :is="item.icon"></component>
        </el-icon>
        <span>{{ item.name }}</span>
      </template>
      <!-- 二级菜单 -->
      <el-menu-item
        v-for="subItem in item.child"
        :key="subItem.id"
        :index="subItem.frontpath"
      >
        <!-- 二级菜单内容 -->
        <template #title>
          <el-icon>
            <component :is="subItem.icon"></component>
          </el-icon>
          <span>{{ subItem.name }}</span>
        </template>
      </el-menu-item>
    </el-sub-menu>
  </el-menu>
</template>

<script setup>
import { Location, Handbag } from "@element-plus/icons-vue";
import { useStore } from "vuex";
import { computed, ref } from "vue";
import { useRoute } from "vue-router";
const route = useRoute();
const store = useStore();

// 定义常量接受仓库中的导航数据
const menus = computed(() => {
  return store.state.menus;
});

const defaultActive = ref(route.path);
</script>

<style lang="less" scoped>
.el-menu {
  height: 100%;
}
// 优化el-menu icon导致的菜单缩放展开卡顿问题
:deep(.el-icon) {
  font-size: 1em;
}
</style>
